

package cn.shengchao.examstar.distribute.infrastrure.service.impl;

import cn.shengchao.examstar.distribute.infrastrure.mapper.DistributeOrderTaskMapper;
import cn.shengchao.examstar.distribute.infrastrure.po.DistributeOrderTask;
import cn.shengchao.examstar.rpc.distribute.enums.DistributeOrderTaskStatusDataStatusEnum;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.shengchao.examstar.distribute.infrastrure.mapper.DistributeOrderTaskLogMapper;
import cn.shengchao.examstar.distribute.infrastrure.po.DistributeOrderTaskLog;
import cn.shengchao.examstar.distribute.infrastrure.service.DistributeOrderTaskLogService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class DistributeOrderTaskLogServiceImpl extends ServiceImpl<DistributeOrderTaskLogMapper, DistributeOrderTaskLog> implements DistributeOrderTaskLogService {
    @Autowired
    DistributeOrderTaskMapper distributeOrderTaskMapper;

    @Transactional
    @Override
    public void logError(Long taskId, Long orderId, String s) {
        DistributeOrderTaskLog taskLog = new DistributeOrderTaskLog();
        taskLog.setOrderId(orderId);
        taskLog.setTaskId(taskId);
        taskLog.setResultCode(1);
        taskLog.setErrorMsg(s);
        baseMapper.insert(taskLog);
        distributeOrderTaskMapper.update(Wrappers.lambdaUpdate(DistributeOrderTask.class)
            .set(DistributeOrderTask::getDataStatus, DistributeOrderTaskStatusDataStatusEnum.STATUS_ERROR.getStatus())
            .eq(DistributeOrderTask::getId, taskId));
    }

    @Transactional
    @Override
    public void logSuccess(Long taskId, Long orderId, String s) {
        DistributeOrderTaskLog taskLog = new DistributeOrderTaskLog();
        taskLog.setOrderId(orderId);
        taskLog.setTaskId(taskId);
        taskLog.setResultCode(0);
        taskLog.setErrorMsg(s);
        baseMapper.insert(taskLog);
        distributeOrderTaskMapper.update(Wrappers.lambdaUpdate(DistributeOrderTask.class)
            .set(DistributeOrderTask::getDataStatus, DistributeOrderTaskStatusDataStatusEnum.STATUS_ASGNED.getStatus())
            .eq(DistributeOrderTask::getId, taskId));
    }
}
